
let tallyId = 1;
let tallyColor = 'blue';
let pgmValue = null;
let pvwValue = null;
const promptText = {"blue": "Standby", "red": "Live", "green": "Preview"}
const tallyText = ["", "①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩"];
$(document).ready(function () {
    // 获取 URL 查询参数部分
    const queryString = window.location.search;
    const params = new URLSearchParams(queryString);

    console.log(params);
    if (params.has('id')) {
        tallyId = Number(params.get('id'));
    }

    initWebSocket();
});

function handleResult(data) {
    console.log(data);
    if (!data) {
        console.log("data is empty");
        return;
    }


    const keys = Object.keys(data);
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        const value = data[key];
        const keySplit = key.split('-');
        if (keySplit[0] === 'pgm' && keySplit[1] === tallyId.toString()) {
            pgmValue = value;
        } else if (keySplit[0] === 'pvw' && keySplit[1] === tallyId.toString()) {
            pvwValue = value;
        }
    }

    if (pgmValue === 0 && pvwValue === 0) {
        tallyColor = 'blue';
    } else if (pgmValue === 1) {
        tallyColor = 'red';
    } else if (pvwValue === 1 && (pgmValue === 0 || pgmValue === null)) {
        tallyColor = 'green';
    }

    $('body').css('background-color', tallyColor);
    $('#tally').text(tallyText[tallyId]);
    $('#prompt').text(promptText[tallyColor]);
}